package com.cssy.yd.logic.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.yd.basic.service.IBasicStorageService;
import com.cssy.yd.logic.entity.LogicWhmoveOutwhDO;
import com.cssy.yd.logic.entity.LogicWhmoveOutwhItemDO;
import com.cssy.yd.logic.service.ILogicStockTransferItemStartService;
import com.cssy.yd.logic.service.ILogicStockTransferStartService;
import com.cssy.yd.logic.util.SqlUtil;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;

@Controller
@RequestMapping("/yd/logic/stockTransferStart")
public class StockTransferStartController extends BaseController<LogicWhmoveOutwhDO>{

	private static final String MANAGER = "/yd/logic/stockTransfer/ManagerStart";
	private static final String EIDT = "/yd/logic/stockTransfer/OperationStart";//添加、修改、显示页面
	private static final String AFFMANAGER = "/yd/logic/stockTransfer/OnRouteAffManager";//添加、修改、显示页面
	@Resource(name = "logicStockTransferStartService")
	private ILogicStockTransferStartService logicStockTransferStartService;
	
	@Resource(name = "logicStockTransferItemStartService")
	private ILogicStockTransferItemStartService logicStockTransferItemStartService;
	
	@Resource(name = "basicwhService")
	private IBasicStorageService basicwhService;
	
	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;
	
	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		model.addAttribute("wh1",basicwhService.findwh(request));
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander=new JqGridHandler(request);
		 SearchConditions scs = new SearchConditions();
		 hander.getWheres(scs);	
		 scs.setOrderBy("dctime desc");
		 Map<String,Object> sqlMap = SqlUtil.sqlUtil.roleWhSqlFilter(request, scUserRolePlaceService,"outwhcode");
		 scs.addCondition((String)sqlMap.get("option"), (List)sqlMap.get("roleWhList"));
		 SearchResult result= logicStockTransferStartService.findListByExample(scs);
		 OutJsonGrid out=new OutJsonGrid(result);
		 out.printJSon(response);
		
	}

	@Override
	public String operationForm(HttpServletRequest request,@PathVariable String id,
			@PathVariable  String flag, Model model) throws Exception {
		model.addAttribute("wh",SqlUtil.sqlUtil.roleWH(request, scUserRolePlaceService));
		model.addAttribute("wh1",basicwhService.findwh(request));
		if(OperationFlag.ADD.equals(flag)){//我新增加时操作
			model.addAttribute("xx", 1);//标示能不能修改内容
		}else{
			LogicWhmoveOutwhDO oi = logicStockTransferStartService.findListById(id);
			if (oi.getBillstatus() == 0){
				model.addAttribute("xx", 1);//标示能不能修改内容
			}
			//List<LogicWhmoveOutwhItemDO> loiilist = logicStockTransferItemStartService.getLogicWhmoveOutwhItemByOtherOutwhId(id);
			model.addAttribute("oi", oi);
			//model.addAttribute("axxJson", loiilist);
		}
		
		return EIDT;
	}
	
	@RequestMapping("/griditems")
	public void griditems(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		if(StringUtils.isNotBlank(id)){
			List<LogicWhmoveOutwhItemDO> loiilist = logicStockTransferItemStartService.getLogicWhmoveOutwhItemByOtherOutwhId(id);
			OutJsonGrid out = new OutJsonGrid(loiilist);
			out.printJSon(response);
		}
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap=BodyMsg.getInstance();
		try{
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			for(String field : fields){
				LogicWhmoveOutwhDO lout=logicStockTransferStartService.findListById(field);
				if(lout.getId()!=null){
					lout.setRemoved(1);
					logicStockTransferStartService.update(lout, SessionUtil.getUser(request));
				}
			}
		
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		}catch(Exception e){
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed()+e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicWhmoveOutwhDO curAP, BindingResult result, Model model)
			throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		String itemdata = request.getParameter("addressInfos");
		if(itemdata==null&&"".equals(itemdata)){
			bodyMap.isFail();
			bodyMap.configWebInfor("物料不能为空！");
			return bodyMap.modelMap;
		}
		try {
			if(curAP.getBillstatus()==0||curAP.getBillstatus()==1){
				logicStockTransferStartService.startsave(curAP, request);  //新增或者提交
			}else{
				logicStockTransferStartService.startCheck(curAP, request);   //二级审核或者三级审核
			}
			bodyMap.isSuccess();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
			bodyMap.configWebId(curAP.getId());
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		return bodyMap.modelMap;

	}
	
	@RequestMapping("/sTAffManager")
	public String sTOnRouteAffManager(HttpServletRequest request, Model model)
			throws Exception {
//		model.addAttribute("wh", basicwhService.findwh(request));
		return AFFMANAGER;
	}

	@RequestMapping("/sTAffList")
	public void sTOnRouteAffList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JqGridHandler hander=new JqGridHandler(request);
		 SearchConditions scs = new SearchConditions();
		 hander.getWheres(scs);	
		 scs.addCondition("lwo.available","1",SearchCondition.OP_NOTEQUAL);
		 scs.addCondition("lwo.billstatus","3",SearchCondition.OP_EQUAL);
		 Map<String,Object> sqlMap = SqlUtil.sqlUtil.roleWhSqlFilter(request, scUserRolePlaceService,"lwo.outwhcode");
		 scs.addCondition((String)sqlMap.get("option"), (List)sqlMap.get("roleWhList"));
		 SearchResult result= logicStockTransferItemStartService.findOnRouteAffListByExample(scs);
		 OutJsonGrid out=new OutJsonGrid(result);
		 out.printJSon(response);
		
	}
	
	
	@RequestMapping(value = "/updateState")
	@ResponseBody
	public Map<String,String> updateState(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		Map<String, String> map=new HashMap<String, String>();
		try{
			String ids = request.getParameter("rows");
			if(ids!=null&&!"".equals(ids)){
				LogicWhmoveOutwhDO lot=logicStockTransferStartService.findListById(ids);
				if(lot.getId()!=null){
					lot.setBillstatus(Short.parseShort("0")); //直接驳回到草稿状态
					logicStockTransferStartService.update(lot, SessionUtil.getUser(request));
					map.put("msg", "SUCCESS");
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			map.put("msg", "ERROR");
		}
		return map;
	}
}
